package com.banggood.pss.mqtools.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

/**
 * @author Mr.chen
 * @date 2020/8/14 15:17
 */
public class ChannelCreator {

    protected static final Log logger = LogFactory.getLog(ChannelCreator.class);

    /**
     * 官方推荐为单连接，多通道模式。
     * 通道在多线程调用的时候需要加锁，否则可能会有错误
     */
    private Connection connection;

    public ChannelCreator(Connection connection) {
        this.connection = connection;
    }

    public Channel getChannel() throws IOException {
        return connection.createChannel();
    }

    public void closeConnect(){
        try {
            connection.close();
        } catch (IOException e) {
            logger.info(e.getMessage(), e);
        }
    }

    public static void closeChannel(Channel channel){
        try {
            channel.close();
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
    }
}
